package com.shihb.structuralpatterns.adapterpatterns;

/**
 *Description:
 *Version:1.0.0
 *@author shihb
 *@date 2020/7/15 20:51
 */
public class ObjectAdapter {

  public static void main(String[] args) {


    Phone2 phone = new Phone2();
    Voltage220 voltage220 = new Voltage220();
    Voltage5 adapter= new VoltageAdapter2(voltage220);
    phone.charging(adapter);
  }
}

//目标
interface Voltage5{
   int out5V();
}

//适配器，继承关系不好，耦合
class VoltageAdapter2 implements Voltage5{
  private Voltage220 voltage220;

  public VoltageAdapter2(Voltage220 voltage220) {
    this.voltage220 = voltage220;
  }

  @Override
  public int out5V() {
    return voltage220.out220v()/44;
  }
}

// 适配对象
class Voltage220{

  public int out220v(){
    int src=220;
    System.out.println("电压是"+src+"V");
    return src;
  }
}

class Phone2{
  void charging(Voltage5 adapter){

    if(adapter.out5V()==5){
      System.out.println("5v充电成功");
    }else {
      System.out.println("不是5v，充电失败");

    }
  }
}
